import { request } from '@/service/request'

// 数据源统计
export interface DataSourceStats {
  total: number
  online: number
  offline: number
  typeDistribution: Record<string, number>
}

// 项目统计
export interface ProjectStats {
  total: number
  active: number
  inactive: number
}

// 作业统计
export interface JobStats {
  total: number
  running: number
  success: number
  failed: number
  todayExecutions: number
}

// 数据资产统计
export interface AssetStats {
  total: number
  weeklyNew: number
  categoryDistribution: Record<string, number>
}

// 数据质量统计
export interface QualityStats {
  totalRules: number
  passedRules: number
  failedRules: number
  qualityScore: number
}

// 元数据统计
export interface MetadataStats {
  totalTables: number
  totalColumns: number
  totalViews: number
  totalIndexes: number
}

// 数据服务统计
export interface ApiStats {
  total: number
  todayCalls: number
  avgResponseTime: number
  successRate: number
}

// 存储统计
export interface StorageStats {
  totalSize: number
  tableCount: number
  databaseDistribution: DatabaseStorage[]
}

export interface DatabaseStorage {
  databaseName: string
  tableCount: number
  storageSize: number
}

// 总体统计
export interface DashboardStats {
  dataSourceStats: DataSourceStats
  projectStats: ProjectStats
  jobStats: JobStats
  assetStats: AssetStats
  qualityStats: QualityStats
  metadataStats: MetadataStats
  apiStats: ApiStats
  storageStats: StorageStats
}

// 获取总体统计数据
export function getDashboardStats() {
  // 模拟数据，实际应该调用后端API
  const mockData: DashboardStats = {
    dataSourceStats: {
      total: 15,
      online: 12,
      offline: 3,
      typeDistribution: {
        'MySQL': 8,
        'PostgreSQL': 3,
        'Oracle': 2,
        'Doris': 2
      }
    },
    projectStats: {
      total: 8,
      active: 6,
      inactive: 2
    },
    jobStats: {
      total: 45,
      running: 8,
      success: 32,
      failed: 5,
      todayExecutions: 156
    },
    assetStats: {
      total: 1250,
      weeklyNew: 28,
      categoryDistribution: {
        '基础数据': 450,
        '业务数据': 380,
        '分析数据': 280,
        '外部数据': 140
      }
    },
    qualityStats: {
      totalRules: 120,
      passedRules: 108,
      failedRules: 12,
      qualityScore: 90.5
    },
    metadataStats: {
      totalTables: 856,
      totalColumns: 12450,
      totalViews: 124,
      totalIndexes: 2340
    },
    apiStats: {
      total: 68,
      todayCalls: 2340,
      avgResponseTime: 245,
      successRate: 99.2
    },
    storageStats: {
      totalSize: 2048.5,
      tableCount: 856,
      databaseDistribution: [
        { databaseName: 'business_db', tableCount: 320, storageSize: 850.2 },
        { databaseName: 'analytics_db', tableCount: 280, storageSize: 650.8 },
        { databaseName: 'external_db', tableCount: 156, storageSize: 380.5 },
        { databaseName: 'archive_db', tableCount: 100, storageSize: 167.0 }
      ]
    }
  }

  return Promise.resolve({ data: mockData })
  
  // 实际的API调用
  // return request<DashboardStats>({
  //   url: '/dataplatform/dashboard/stats',
  //   method: 'get'
  // })
}

// 获取数据源统计
export function getDataSourceStats() {
  return request<DataSourceStats>({
    url: '/dataplatform/dashboard/datasource-stats',
    method: 'get'
  })
}

// 获取项目统计
export function getProjectStats() {
  return request<ProjectStats>({
    url: '/dataplatform/dashboard/project-stats',
    method: 'get'
  })
}

// 获取作业统计
export function getJobStats() {
  return request<JobStats>({
    url: '/dataplatform/dashboard/job-stats',
    method: 'get'
  })
}

// 获取数据资产统计
export function getAssetStats() {
  return request<AssetStats>({
    url: '/dataplatform/dashboard/asset-stats',
    method: 'get'
  })
}

// 获取数据质量统计
export function getQualityStats() {
  return request<QualityStats>({
    url: '/dataplatform/dashboard/quality-stats',
    method: 'get'
  })
}

// 获取元数据统计
export function getMetadataStats() {
  return request<MetadataStats>({
    url: '/dataplatform/dashboard/metadata-stats',
    method: 'get'
  })
}

// 获取数据服务统计
export function getApiStats() {
  return request<ApiStats>({
    url: '/dataplatform/dashboard/api-stats',
    method: 'get'
  })
}

// 获取存储统计
export function getStorageStats() {
  return request<StorageStats>({
    url: '/dataplatform/dashboard/storage-stats',
    method: 'get'
  })
}